<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AssemblyScript SDK Example</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
<script>
  require([ "https://cdn.jsdelivr.net/npm/assemblyscript@latest/dist/sdk.js" ], ({ asc }) => {
    asc.ready.then(() => {
      console.log("Running simple example...");
      simpleExample(asc);
      console.log("\nRunning extended example...");
      extendedExample(asc);
    });
  });

  const SOURCE_CODE = `export function test(): void {}`;

  // This uses `asc.compileString`, a convenience API useful if all one wants to
  // do is to quickly compile a single source string to WebAssembly.
  function simpleExample(asc) {
    const { text, binary } = asc.compileString(SOURCE_CODE, {
      optimizeLevel: 3,
      runtime: "none"
    });
    console.log(`>>> TEXT >>>\n${text}`);
    console.log(`>>> BINARY >>>\n${binary.length} bytes`);
  }

  // The full API works very much like asc on the command line, with additional
  // environment bindings being provided to access the (virtual) file system.
  function extendedExample(asc) {
    const stdout = asc.createMemoryStream();
    const stderr = asc.createMemoryStream();
    asc.main([
      "module.ts",
      "-O3",
      "--runtime", "none",
      "--binaryFile", "module.wasm",
      "--textFile", "module.wat",
      "--sourceMap"
    ], {
      stdout,
      stderr,
      readFile(name, baseDir) {
        return name === "module.ts" ? SOURCE_CODE : null;
      },
      writeFile(name, data, baseDir) {
        console.log(`>>> WRITE:${name} >>>\n${data.length}`);
      },
      listFiles(dirname, baseDir) {
        return [];
      }
    }, err => {
      console.log(`>>> STDOUT >>>\n${stdout.toString()}`);
      console.log(`>>> STDERR >>>\n${stderr.toString()}`);
      if (err) {
        console.log(">>> THROWN >>>");
        console.log(err);
      }
    });
  }
</script>
<p>See the browser console!</p>
</body>
</html>
